package top.yrlby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import top.yrlby.dto.DishDto;
import top.yrlby.pojo.Dish;
import top.yrlby.dao.DishMapper;
import top.yrlby.pojo.DishFlavor;
import top.yrlby.service.DishFlavorService;
import top.yrlby.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author baiyang
 * @since 2022-09-05
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品,同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);//调用MP的新增方法添加dish表信息
        Long dishId = dishDto.getId();//获取菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();    //获取菜品口味
        //使用Stream流完成添加多个菜品口味
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
//        System.out.println(dishDto.toString());
        this.updateById(dishDto);//调用MP的修改方法修改dish表信息
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        //构建查询条件
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        //where
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //调用MP的delete方法
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        ////获取口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //使用Stream流完成添加多个菜品口味
        flavors.stream().map(item->{
            item.setDishId(dishDto.getId());
            return item;
                }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询修改的菜品信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);//查询菜品基本信息，从dish表查询
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);//拷贝查询出来的dish信息
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        // 查询dish_flavor表信息
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);//保存实体类中
        return dishDto;
    }
}
